﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QLNH_DAL_WS
{
    public class BillDAO
    {
        public List<BillDTO> GetAllBill()
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var result = from b in data.BILLs
                         select new BillDTO()
                         {
                             ID = b.ID,
                             CreatedDate = DateTime.Parse(b.CreatedDate.ToString()),
                             Total = double.Parse(b.Total.ToString())
                         };

            if (result.Count() > 0)
            {
                return result.ToList<BillDTO>();
            }

            return null;
        }


        public List<BillDTO> GetAllBill(DateTime start, DateTime end)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var result = from b in data.BILLs
                         where b.CreatedDate.Value.Date >= start.Date && b.CreatedDate.Value.Date <= end.Date
                         select new BillDTO()
                         {
                             ID = b.ID,
                             CreatedDate = DateTime.Parse(b.CreatedDate.ToString()),
                             Total = double.Parse(b.Total.ToString())
                         };

            if (result.Count() > 0)
            {
                return result.ToList<BillDTO>();
            }

            return null;
        }

        public bool InsertBill(List<BillDetailDTO> billDetail)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            // lay ma cuoi cung
            var lastID = from b in data.BILLs
                         orderby b.ID descending
                         select b.ID;

            // lay ma prefix
            var billCode = (from p in data.CODEs
                            where p.Data.Equals("bill")
                            select p).First();

            string id = billCode.Prefix;

            if (lastID.Count() > 0)
            {
                string tmp = lastID.First();
                id = Ultilities.GetNextId(tmp, "bill");
            }
            else
            {
                for (int i = 0; i < Int32.Parse(billCode.PostfixLength.ToString()); i++)
                {
                    id += "0";
                }
            }

            BILL bill = new BILL() { ID = id, CreatedDate = DateTime.Now };
            double total = 0;

            for (int i = 0; i < billDetail.Count; i++)
            {
                bill.BILL_DETAILs.Add(new BILL_DETAIL()
                {
                    BillID = id,
                    MealID = billDetail[i].MealID,
                    Price = billDetail[i].Price,
                    Amount = billDetail[i].Amount
                });
                total += billDetail[i].Amount * billDetail[i].Price;
            }
            bill.Total = total;

            try
            {
                data.BILLs.InsertOnSubmit(bill);
                data.SubmitChanges();

                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}